var app = new Vue({
	el: '#app',
	data: {
		// 展示的容器名
		showCube: "medicine-view",
		// 弹出框开关控制
		dialog: {
			medicineDamage: false,
			medicineDetail: false,
			changePassword: false
		},
		// 密码管理
		password: {
			oldPassword: "",
			newPassword1:"",
			newPassword2:""
		},
		// 保存用户信息
		userMsg: {
			isLogin: false,
			account: "",
			name: "",
			id: ""
		},
		// 原始的所有药品列表
		medicineMsg: [],
		// 展示药品详情
		medicineDetail: {},
		// 新增药品的属性
		medicineAdd: {
			name: "",
			amout: "",
			price: "",
			introduce: "",
			imgurl: "",
			producetime: "",
			producetimetemp: "",
			abletime: "",
			code: "",
			company: "",
			address: "",
			phone: "",
			type: ""
		},
		// 选中的销售药品
		medicineMsgSelected: {},
		// 销售药品表单信息
		medicineDamageMsg: {
			amout: 1,
			remarks: "",
			message: ""
		},
		// 原始的所有账单列表
		billMsg: [],
	},
	computed: {
		// 显示的药品列表
		showMedicineAble: function(){
			var ret = [];
			for (var i = 0; i < this.medicineMsg.length; i++) {
				if (this.medicineMsg[i].able == 1) {
					ret.push(this.medicineMsg[i]);
				}
			}
			return ret;
		},
		// 显示的销售账单
		showDamageBill: function(){
			var ret = [];
			for (var i = 0; i < this.billMsg.length; i++) {
				if (this.billMsg[i].type == 'damage' && this.billMsg[i].clerk_id == this.userMsg.id) {
					this.billMsg[i].amout = -this.billMsg[i].amout;
					ret.push(this.billMsg[i])
				}
			}
			return ret;
		},
	},
	methods: {
		// 点击左边栏的时候，切换显示的容器
		changeCube: function(param){
			this.showCube = param;
		},
		// 获取登录状态
		getLoginStatus: function(){
			var self = this;
			$.get('/api/isLogin',function(data,status){
				if (data.success == "ok") {
					self.userMsg.isLogin = true;
					self.userMsg.account = data.result.account;
					self.userMsg.name = data.result.name;
					self.userMsg.id = data.result.id;
				} else {
					window.location.href = "/"
				}
			})
		},
		logout: function(){
			var self = this;
			$.post("/api/logout",{},function(data,status){
				if (data.success = "ok") {
					self.getLoginStatus();
				}
			});
		},
		// 获取所有账单
		getBill: function(){
			var self = this;
			var data = {};
			$.post("/api/viewBill",data,function(data, status){
				if (data.success == "ok") {
					self.billMsg = data.result.data;
				}
			})
		},
		// 获取所有药品
		getMedicine: function(){
			var self = this;
			var data = {};
			$.post("/api/viewMedicine",data,function(data, status){
				if (data.success == "ok") {
					self.medicineMsg = data.result.data;
				}
			})
		},
		// 弹出药品详细详情框
		showMedicineDetail: function(a,b){
			this.medicineDetail = b;
			this.dialog.medicineDetail = true;
		},
		// 计算到期时间
		caleAbletime: function(row,column){
			var ret = new Date((row.producetime+(row.abletime*(30*24*3600)))*1000).toLocaleDateString();
			if ( (new Date((row.producetime+(row.abletime*(30*24*3600)))*1000) - new Date()) < (15*24*3600*1000) ) {
				ret += '（即将过期）'
			}
			return ret;
		},
		// 时间格式化
		timeParse: function(row,column){
			var ret ;
			if (row.create_time != undefined) {
				ret = new Date(row.create_time).toLocaleString();
			} else if (row.createtime != undefined) {
				ret = new Date(row.createtime).toLocaleString();
			}
			return ret;
		},
		// 创建药品
		addMedicine: function(){
			var self = this;
			this.medicineAdd.producetime = this.medicineAdd.producetimetemp.valueOf()/1000;


			if (this.medicineAdd.name == "") {
				swal("保存失败", "名字不能为空", "error")
			} else if ( this.medicineAdd.price == "") {
				swal("保存失败", "价格不能为空", "error")
			} else if ( this.medicineAdd.introduce == "") {
				swal("保存失败", "介绍不能为空", "error")
			} else if ( this.medicineAdd.imgurl == "") {
				swal("保存失败", "图片不能为空", "error")
			} else if ( this.medicineAdd.producetime == "") {
				swal("保存失败", "生产日期不能为空", "error")
			} else if ( this.medicineAdd.type == "") {
				swal("保存失败", "药品类型不能为空", "error")
			} else if ( this.medicineAdd.abletime == "") {
				swal("保存失败", "有效期不能为空", "error")
			} else if ( this.medicineAdd.company == "") {
				swal("保存失败", "公司不能为空", "error")
			} else if ( this.medicineAdd.address == "") {
				swal("保存失败", "地址不能为空", "error")
			} else if ( this.medicineAdd.phone == "") {
				swal("保存失败", "电话不能为空", "error")
			} else if ( this.medicineAdd.code == "") {
				swal("保存失败", "注册代码不能为空", "error")
			} else {

				var data = {
					name: this.medicineAdd.name,
					type: this.medicineAdd.type,
					price: this.medicineAdd.price,
					introduce: this.medicineAdd.introduce,
					imgurl: this.medicineAdd.imgurl,
					producetime: this.medicineAdd.producetime,
					abletime: this.medicineAdd.abletime,
					company: this.medicineAdd.company,
					address: this.medicineAdd.address,
					phone: this.medicineAdd.phone,
					code: this.medicineAdd.code
				}

				$.post("/api/addMedicine",data,function(data, status){
					if (data.success == "ok") {
						swal("保存成功", "药品已添加", "success");
						self.getMedicine();
					} else {
						swal("保存失败", data.msg, "error");
					}
				})
			}
		},
		// 图片上传
		uploadImg: function(){
			var self = this;
			var formData = new FormData($("#uploadForm")[0])
			$.ajax({    
				url: '/api/upload' ,
				type: 'POST',    
				data: formData,    
				async: false,    
				cache: false,    
				contentType: false,    
				processData: false,    
				success: function (result) {    
					self.medicineAdd.imgurl = result.file_path;
				},    
				error: function (result) { 
					swal("上传失败","","error");
				}    
			}); 
		},
		// 弹出报损药品弹框
		showDamageMedicine: function(a, b){
			this.medicineMsgSelected = b;
			this.dialog.medicineDamage = true;
		},
		// 弹出更改密码弹框
		showChangePassword: function(){
			this.dialog.changePassword = true;
		},
		// 报损药品
		damageMedicine: function(){
			var self = this;
			var data = {
				type: 'damage',
				medicine_id: self.medicineMsgSelected.id,
				amout: self.medicineDamageMsg.amout,
				remarks: self.medicineDamageMsg.remarks,
				message: self.medicineDamageMsg.message,
				create_time: self.medicineDamageMsg.create_time
			}
			$.post("/api/addBill",data, function(data, status){
				if (data.success == "ok") {
					self.medicineDamageMsg.amout = self.medicineDamageMsg.remarks = self.medicineDamageMsg.message = "";
					self.dialog.medicineDamage = false;
					swal("操作成功", "", "success");
					self.getMedicine();
					self.getBill();
				} else {
					swal("操作失败", data.msg, "error");
				}
			})
		},
		// 修改密码
		changePassword: function(){
			var self = this;
			if (this.password.oldPassword == "") {
				swal("原始密码不能为空","","error");
			} else if (this.password.newPassword1 == "") {
				swal("新密码不能为空","","error");
			} else if (this.password.newPassword1.length < 10) {
				swal("新密码不能少于10位","","error");
			} else if (this.password.newPassword1 != this.password.newPassword2) {
				swal("两次输入不一样","","error");
			} else {
				var data = {
					oldPassword: self.password.oldPassword,
					newPassword: self.password.newPassword1,
					account: self.userMsg.account,
					type: 'clerk'
				}
				$.post("/api/changePassword",data, function(data,status){
					if (data.success == "ok") {
						self.password.oldPassword = self.password.newPassword1 = self.password.newPassword2 = "";
						self.dialog.changePassword = false;
						swal("修改成功", "密码已更新", "success");
					} else {
						swal("修改失败", data.msg, "error");
					}
				});
			}
		}
	},
	mounted: function(){
		var self = this;
		this.getLoginStatus();
		this.getMedicine();
		this.getBill();
		setTimeout(function(){
			$("#inputFile").change(function(){
				self.uploadImg();
			})
		},2000)
		
	}
})